package Experiment;

/**
 * Created by user on 2017/6/7.
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;

import static Experiment.ThreeDES.decryptMode;

public class SocketService {
    //搭建服务器端
    public static void main(String[] args) throws IOException{
        SocketService socketService = new SocketService();
        //1、a)创建一个服务器端Socket，即SocketService
        socketService.oneServer();
    }
    public  void oneServer(){
        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(8080);
                //b)指定绑定的端口，并监听此端口。
                System.out.println("服务器启动成功");
                //创建一个ServerSocket在端口5209监听客户请求
            }catch(Exception e) {
                System.out.println("没有启动监听："+e);
                //出错，打印出错信息
            }
            Socket socket=null;
            try{
                socket=server.accept();
                //2、调用accept()方法开始监听，等待客户端的连接
                //使用accept()阻塞等待客户请求，有客户
                //请求到来则产生一个Socket对象，并继续执行
            }catch(Exception e) {
                System.out.println("Error."+e);
                //出错，打印出错信息
            }
            //3、获取输入流，并读取客户端信息
            String line;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //由Socket对象得到输入流，并构造相应的BufferedReader对象
            PrintWriter writer=new PrintWriter(socket.getOutputStream());
            //由Socket对象得到输出流，并构造PrintWriter对象
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            //由系统标准输入设备构造BufferedReader对象
           Dncode dncode = new Dncode();
            String line2 = in.readLine();
            String line3 = in.readLine();
            System.out.println("得到的MD5值为"+line3);
            System.out.println("得到的密文为"+line2);
            String encodeRules = "67,-78,48,-68,-9,63,41,-42,70,-109,24,74,97,-8,71,92,123,46,-80,-123,-100,-29,-120,49,-39,55,-100,-31,62,-46,39,42,-10,-126,-88,-59,-86,-22,67,-95,-81,-122,-107,114,-31,113,-17,110,26,122,-29,57,-27,-70,-67,40,-108,14,-17,8,1,-71,-93,-1,-22,-87,52,12,-29,61,-117,126,73,-69,-62,-48,-38,-128,7,77,95,-88,-22,88,-5,-55,-99,39,107,-128,67,-120,49,-127,-82,-12,-117,13,73,105,-45,118,122,1,-124,123,-18,65,47,-40,105,-30,111,51,13,-83,22,-89,114,-62,-55,98,-15,-8,40,36,37,-88,\n";
            String content = line2;
            System.out.println("根据DH生成的密钥"+encodeRules+"解密后的明文是:"+dncode.AESDncode(encodeRules, content));
            String x=dncode.AESDncode(encodeRules, content);
            MessageDigest m=MessageDigest.getInstance("MD5");
            m.update(x.getBytes("UTF8"));
            byte s[ ]=m.digest( );
            String result1="";
            for (int i=0; i<s.length; i++){
                result1+=Integer.toHexString((0x000000ff & s[i]) |
                        0xffffff00).substring(6);
            }
           if (!result1.equals(line3)) {
               System.out.println("密文被篡改");
               System.exit(0);
           }
            MyDC dc = new MyDC();
            int result = dc.evaluate (new String(dncode.AESDncode(encodeRules, content)));
            System.out.println(result);

            //在标准输出上打印从客户端读入的字符串
            line=String.valueOf(result);
            //从标准输入读入一字符串
            //4、获取输出流，响应客户端的请求
            while(!line.equals("end")){
                //如果该字符串为 "bye"，则停止循环
                writer.println(String.valueOf(result));
                //向客户端输出该字符串
                writer.flush();
                //刷新输出流，使Client马上收到该字符串
                System.out.println("Server:"+line);
                //在系统标准输出上打印读入的字符串
                System.out.println("Client:"+in.readLine());
                //从Client读入一字符串，并打印到标准输出上
                line=br.readLine();
                //从系统标准输入读入一字符串
            } //继续循环

            //5、关闭资源
            writer.close(); //关闭Socket输出流
            in.close(); //关闭Socket输入流
            socket.close(); //关闭Socket
            server.close(); //关闭ServerSocket
        }catch(Exception e) {//出错，打印出错信息
            System.out.println("Error."+e);
        }
    }
}